package com.hollycrm.hollysqm.handle.service.v8.impl;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.hollycrm.hollysqm.entities.TblQmPlanParam;
import com.hollycrm.hollysqm.handle.service.PlanChain;

/**
 * 用户级别
 * @author jianglong
 * @date 2017年3月22日 下午3:54:12
 */
@Service("custLevelService")
public class CustLevelServiceImpl  extends PlanChain {
	
	private static final Logger log = LoggerFactory.getLogger(CustLevelServiceImpl.class);
	
	@Autowired
    private JdbcTemplate jdbc;
	
	/**
	 * 主方法 
	 */
	@Override
	public String applyRule(TblQmPlanParam bean, Map<String, Object> paramMap) throws Exception {
		String custLevel =(String) paramMap.get("custLevel");
		if (StringUtils.isEmpty(custLevel)){
			return null;
		}
		String agentCode = (String) paramMap.get("agentCode");
//		log.info("计划抽取 >> 用户级别  过滤 ...");
		String sql = "delete e from tbl_qm_plan_extract e where e.plan_id ='%s' and e.agent_code='%s' and exists ("
				+ " select t.custcontinfo_id from tbl_voc_custcontinfo t where " 
				+ " t.cust_level not in (%s) and t.custcontinfo_id = e.data_id)";					
		int count = jdbc.update(String.format(sql, bean.getPlanId(),agentCode, custLevel));
		log.info("【"+bean.getPlanId()+"】计划抽取 >> 用户级别  过滤【"+count+"】条 ...");
		return null;
	}

}
